export const HOOKS = [
  "beforeCreate",
  "created",
  "beforeMount",
  "mounted",
  "beforeUpdate",
  "updated",
  "beforeDestory",
  "destoryed"
];

// 策略模式(HOOKS里面的会合并，其它的后者会覆盖前者)
const starts = {};
starts.data = function (parent, child) { return child }
starts.computed = function (parent, child) { return child }
starts.watch = function (parent, child) { return child }
starts.methods = function (parent, child) { return child }
HOOKS.forEach(key => {
  starts[key] = mergeHook;
})

function mergeHook(parentVal, childVal) {
  if (childVal) {
    if (parentVal) {
      return parentVal.concat(childVal);
    } else {
      return [childVal];
    }
  } else {
    return parentVal;
  }
}

export function mergeOptions(parent, child) {
  const options = {};
  // 有parent
  for (let key in parent) {
    mergeFiled(key);
  }
  // 有child
  for (let key in child) {
    mergeFiled(key);
  }

  function mergeFiled(key) {
    // 如果是在预设的starts的hooks里面的生命周期，会进行合并，若是其它，mixin里面的会被覆盖
    if (starts[key]) {
      options[key] = starts[key](parent[key], child[key]);
    } else { // 非starts里面的直接取child的值
      options[key] = child[key] || parent[key];
    }
  }
  return options;
}